<?php

declare (strict_types = 1);

namespace app\api\controller;

use app\api\validate\UserValidate;
use app\common\service\file\FileUploadsService;
use app\common\service\system\ProvinceService;
use app\common\service\system\RegionService;
use app\common\service\user\UserService;
use app\common\service\VersionService;
use app\common\util\ApiResponseTrait;
use think\Request;

class Common {

    use ApiResponseTrait;

    protected Request $request;
    
    /**
     * 构造函数、依赖注入
     *
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 登录注册
     *
     * @param UserService $service
     * @param UserValidate $userValidate
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login(UserService $service, UserValidate $userValidate,): \think\Response
    {
        $data = $this->request->post();

        if (!$userValidate->sceneLogin()->check($data)) {
            return $this->notValidate($userValidate->getError());
        }

        return $this->success($service->login($data));
    }

    /**
     * 发送验证码
     *
     * @param UserService $service
     * @param UserValidate $userValidate
     * @return \think\Response
     */
    public function sendCode(UserService $service, UserValidate $userValidate): \think\Response
    {
        $phone = $this->request->post('phone');

        if (!$userValidate->sceneSendCode()->check(['phone' => $phone])) {
            return $this->notValidate($userValidate->getError());
        }

        return $this->success($service->sendCode($phone));
    }

    /**
     * 获取协议隐私政策|用户协议
     *
     * @return \think\Response
     */
    public function getAgreement(): \think\Response
    {
        $type = $this->request->get('type');
        return $this->success(get_config('agreement_config.' . $type));
    }

    /**
     * 检查应用最新版本
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkVersion(): \think\Response
    {
        $version = $this->request->get('version', 0);
        $versionService = app()->make(VersionService::class);
        return $this->success($versionService->checkVersion($version));
    }

    /**
     * 上传
     *
     * @return \think\Response
     * @throws \Exception
     */
    public function upload(): \think\Response
    {
        $fileService = app()->make(FileUploadsService::class);
        return $this->success($fileService->upload());
    }

    /**
     * 下载
     *
     * @return \think\response\File|\think\Response
     */
    public function download(): \think\response\File|\think\Response
    {
        $filename = $this->request->get('filename');
        $type = $this->request->get('type');
        // 文件存储的实际路径，这里以public目录下的uploads为例
        $filePath = public_path() . "storage/{$type}/" . $filename;

        // 检查文件是否存在
        if (!file_exists($filePath)) {
            return $this->notValidate('文件不存在');
        }

        // 返回文件下载响应
        return download($filePath);
    }

    /**
     * 获取三级联动
     *
     * @return \think\Response
     */
    public function getTreeAreaList(): \think\Response
    {
        $service = app()->make(RegionService::class);
        return $this->success($service->getTreeAreaList());
    }
}